package com.yyq.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyq.eduorder.Client.EduClinet;
import com.yyq.eduorder.Client.UcenterClinet;
import com.yyq.eduorder.entity.Order;
import com.yyq.eduorder.service.OrderService;
import com.yyq.eduorder.utils.OrderNoUtil;
import com.yyq.result.R;
import com.yyq.result.vo.UcenterMemberOrder;
import com.yyq.result.vo.courseInfoVoOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author yyq
 * @since 2021-05-06
 */
@CrossOrigin
@RestController
@RequestMapping("/eduorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private EduClinet eduClinet;
    @Autowired
    private UcenterClinet ucenterClinet;
    //0　查找客户已经购买的课程
    //参数　用户id
    @GetMapping("getUserOrder/{user_id}")
    public R getUserOrder(@PathVariable String user_id){
        int total=0;
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("member_id",user_id);
        wrapper.eq("status",1);
        List<Order> list = orderService.list(wrapper);
        if (list!=null)
        total= list.size();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items",list);
        map.put("total",total);

        return R.ok().data(map);

    }

    //1　生成订单　需要课程id 用户ｉｄ 返回订单号
    @GetMapping("getOrderNo/{courseId}/{memberId}")
    public R getOrderNo(@PathVariable String courseId,@PathVariable String memberId){
        //根据课程ｉｄ获取课程
        courseInfoVoOrder courseInfoOrder = eduClinet.getCourseInfoOrder(courseId);
        //根据用户ｉｄ获取用户
        UcenterMemberOrder userInfoOrder = ucenterClinet.getUserInfoOrder(memberId);
        //检查数据库是否存在该课程对应该用户的课程订单
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        Order one = orderService.getOne(wrapper);
        String orderNo;
        if(one==null){
        Order order=new Order();
        //生成订单号
        order.setOrderNo(OrderNoUtil.getOrderNo());  //获取订单号
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);               //支付状态
        order.setPayType(1);              //支付类型　１微信
        orderService.save(order);
        orderNo=order.getOrderNo();
        }
        else {

            orderNo=one.getOrderNo();
        }
        return R.ok().data("orderNo",orderNo);
    }

    //２根据订单号获取订单信息
    @GetMapping("getOrderInfo/{orderNo}")
    public R getOrderInfo(@PathVariable String orderNo){
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        Order one = orderService.getOne(wrapper);
        return R.ok().data("order",one);
    }

    //３　根据课程号　用户　　查询课程是否购买
    @GetMapping("getOrderState/{courseId}/{memberId}")
    public R getOrderState(@PathVariable String courseId,@PathVariable String memberId){
        int flag=0;
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        Order one = orderService.getOne(wrapper);
        if(one==null)  flag=0;
        else if(one.getStatus()==1) flag=1;
        return R.ok().data("flag",flag);
    }
    //4　获取未支付的订单列表
    @GetMapping("getNoPayOrder/{memberId}")
    public R getNoPayOrder(@PathVariable String memberId){
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",0); //0未支付
        List<Order> list = orderService.list(wrapper);
        return R.ok().data("order",list);
    }
    //5删除课程
    @DeleteMapping("deleteByOrderNo/{orderNo}")
    public R deleteByOrderNo(@PathVariable String orderNo)
    {
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        Order one = orderService.getOne(wrapper);
        orderService.removeById(one.getId());
        return R.ok();
    }


}

